<template>
  <div id="box">
    <div class="loginBox">
      <!-- 头部区域 -->
      <div class="logo">
        <img src="../../assets/logo.png" alt="" />
      </div>
      <!-- 表单区域 -->
      <el-form
        ref="loginFormRef"
        :rules="loginFormRules"
        :model="loginFormData"
        status-icon
      >
        <el-form-item prop="username">
          <el-input
            prefix-icon="el-icon-user-solid"
            clearable
            v-model="loginFormData.username"
          ></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            prefix-icon="el-icon-lock"
            clearable
            v-model="loginFormData.password"
            show-password
          ></el-input>
        </el-form-item>
        <div class="btn">
          <el-button type="primary" @click="submitForm">提交</el-button>
          <el-button type="info" @click="resetForm">重置</el-button>
        </div>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      loginFormData: {
        username: 'admin',
        password: '123456'
      },
      loginFormRules: {
        username: [
          { required: true, message: '请输入账号', trigger: 'blur' },
          { min: 3, max: 10, message: '长度不合法', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 3, max: 10, message: '长度不合法', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    // 重置表单
    resetForm() {
      this.$refs.loginFormRef.resetFields()
    },
    // 提交表单
    submitForm() {
      this.$refs.loginFormRef.validate(async valid => {
        // 预验证不通过 返回
        if (!valid) {
          return false
        }
        // 预验证通过 调用接口
        const { data } = await this.$http.post('login', this.loginFormData)
        console.log(data)
        if (data.meta.status !== 200) {
          return this.$message({
            message: '登录失败',
            type: 'error'
          })
        }
        this.$message({ message: '登录成功', type: 'success' })
        // 将token保存到sessionStorage中
        window.sessionStorage.setItem('token', data.data.token)
        // 编程式导航到/home
        this.$router.push('/home')
      })
    }
  }
}
</script>

<style lang="less" scoped>
#box {
  background-color: #2b4b6b;
  //使用flex布局
  display: flex;
  //垂直方向轴
  flex-direction: column;
  // 中间对齐
  justify-content: center;
  height: 100%;
}
.loginBox {
  // 不能在这里使用flex布局，会影响form表单，只能从logo修改
  position: relative;
  margin: 0 auto;
  height: 300px;
  width: 500px;
  background-color: #fff;
}
.logo {
  position: absolute;
  transform: translate(-50%, -60%);
  left: 50%;
  height: 120px;
  width: 120px;
  border: 1px solid #ccc;
  padding: 10px;
  border-radius: 50%;
  background-color: #eee;
  overflow: hidden;
  box-shadow: 0px 0px 10px 5px #ddd;
  img {
    height: 120px;
    width: 120px;
    border: 1px solid #ddd;
    border-radius: 50%;
  }
}
.el-form {
  padding: 0 35px;
  position: relative;
  margin-top: 100px;
  .el-form-item {
    height: 40px !important;
    .el-input {
      width: 100%;
    }
  }
  .el-button {
    height: 40px;
  }
}
.btn {
  float: right;
  margin-right: 20px;
}
</style>
